<template>
  <basic-container v-loading="loading">
    <div class="el-page-header haruyuki-mb-20">
      <div
        class="el-page-header__left haruyuki-flex-center haruyuki-size-18"
        @click="goBackPage"
      >
        <i class="el-icon-arrow-left"></i>
        <div class="haruyuki-size-16">返回</div>
      </div>
      <div class="el-page-header__content">{{ $route.name }}</div>
    </div>
    <div class="haruyuki-bg-color-white">
      <div class="merchant-net-steps">
        <el-steps :active="active" align-center>
          <el-step v-for="step in activeStepList" :key="step.active">
            <template slot="title">
                <div class="merchant-net-pointer" @click="jumpStepSkip(step.active)">{{ step.title }}</div>
            </template>
            <template slot="icon">
                <div class="el-step__icon-inner merchant-net-pointer" @click="jumpStepSkip(step.active)">{{ step.iconText }}</div>
            </template>
          </el-step>
        </el-steps>
      </div>
      <!-- <el-divider></el-divider> -->
      <template>
        <div v-show="active == 0">
          <basic-form :basicForm="basicForm" @userTypeChange="getUserType" ref="toBasicForm" />
        </div>
      </template>
      <template>
        <div v-show="active == 1">
          <card-form
            :cardForm="cardForm"
            :user-type="merUserType"
            ref="toCardForm"
          />
        </div>
      </template>
      <template>
        <div v-show="active == 2">
          <rate-form
            :merchantId="merchantId"
            :unionpayData="unionpayData"
            ref="toRateForm"
          />
          <!-- <unionpay-policy :data="unionpayData" /> -->
        </div>
        <!-- <div v-if="active == 2">
          <unionpay-policy :merchantId="merchantId" @codeAdvanceRate="getCodeAdvanceRate" />
        </div> -->
      </template>
      <template>
        <div v-show="active == 3">
          <shop-form :merchantId="merchantId" :shopForm="shopForm" ref="toShopForm" />
        </div>
      </template>
      <template v-if="active == 4">
        <merchant-preview :merchantForm="merchantForm" />
      </template>
      <span class="haruyuki-flex-x-end">
        <el-button
          icon="el-icon-news"
          size="medium"
          type="info"
          v-if="active != 0 && active != 4"
          v-preventClick
          @click="handlePageStorage"
          >暂 存</el-button
        >
        <el-button
          icon="el-icon-top"
          size="medium"
          type="primary"
          v-if="active != 0"
          v-preventClick
          @click="handlePageUp"
          >上一页</el-button
        >
        <el-button
          :icon="
            dialogSubmitBtnText == '下一页' ? 'el-icon-bottom' : 'el-icon-check'
          "
          size="medium"
          type="primary"
          plain
          v-preventClick
          @click="handlePageDown"
          >{{ dialogSubmitBtnText }}</el-button
        >
      </span>
    </div>
  </basic-container>
</template>

<script>
import {
  addFirstStep,
  addSecondStep,
  addThirdStep,
  addFourthStep,
  updateFirstStep,
  updateSecondStep,
  updateThirdStep,
  updateFourthStep,
  getFirstStepVO,
  getSecondStepVO,
  getThirdStepVO,
  getFourthStepVO,
  getFifthStepVO,
  getFifthFlow,
  getSecondBankList,
  saveStepSto
} from "@/api/rb-oem/posinfo/pos_info";
import { getThirdStepAttachPlus } from "@/api/rb-oem/serviceinfo/service_info";
import basicForm from "@/components/rb-merchant/basic-form";
import cardForm from "@/components/rb-merchant/card-form";
import rateForm from "@/components/rb-merchant/rate-form";
// import unionpayPolicy from "@/components/rb-merchant/unionpay-policy";
import shopForm from "@/components/rb-merchant/shop-form";
import merchantPreview from "@/components/rb-merchant/merchant-preview";
export default {
  data() {
    return {
      dialogType: "", //打开类型 add/edit
      dialogText: "",
      dialogSubmitBtnText: "下一页",
      merchantType: '',
      merchantId: "null",
      active: 0,
      activeStepList: [
        {title: '基本信息', iconText: '1', active: 0},
        {title: '结算信息', iconText: '2', active: 1},
        {title: '费率信息', iconText: '3', active: 2},
        {title: '协议信息', iconText: '4', active: 3},
        {title: '预览提交', iconText: '5', active: 4},
      ],
      loading: false,
      merUserType: '',
      basicForm: {
        userType: "",
        ysMerchantKey: "",
        industryCode: "",
        mccCd: "",
        userShortName: "",
        accountManager: "",
        isLegal: "1",
        handHeld: "",
        cardFront: "",
        cardReverse: "",
        legalPersonName: "",
        crpGender: "",
        legalPersonPhone: "",
        crpProfession: "",
        legalPersonId: "",
        legalPersonIdSdate: "",
        legalPersonIdEdate: "",
        legalProvinceCityArea: [],
        legalProvince: "",
        legalCity: "",
        legalArea: "",
        mercProvinceCityArea: [],
        mercProvCode: "",
        mercCityCode: "",
        mercAreaCode: "",
        serNo: "",
        crpAddr: "",
        licensePic: "",
        registeredName: "",
        businessLicence: "",
        businessLicenceNo: "",
        businessLicenceSDate: "",
        businessLicenceEDate: "",
        contactsName: "",
        contactsPhone: "",
        contactsEmail: "",
        authLetterPic: "",
        contactFrontPic: "",
        contactReversePic: "",
        contactPersonId: "",
        idValiddateBegin: "",
        idValiddateEnd: "",
        contactsAddr: "",
        remark: "",
      },
      cardForm: {
        id: null,
        balanceAccType: "",
        accountFront: "",
        accountReverse: "",
        billingAttorney: "",
        openPermit: "",
        ecardFront: "",
        ecardReverse: "",
        bankType: "",
        openAccBankType: "",
        accProvinceCityArea: "",
        openAccProvinceCode: "",
        unincorporatedName: "",
        unincorporatedId: "",
        openAccCityCode: "",
        openAccAreaCode: "",
        bankMobile: "",
        balanceAccName: "",
        balanceAccCardNo: "",
      },
      rateForm: {
        id: null,
        codeScanningRate: "",
        codeAdvanceOpen: "0",
        codeAdvanceRate: "",
        vo: [],
      },
      codeAdvanceRate: "",
      shopForm: {
        id: null,
        merchantDoor: "",
        interior: "",
        checkoutCounter: "",
      },
      unionpayData: [],
      merchantForm: {},
    };
  },
  components: {
    basicForm,
    cardForm,
    rateForm,
    // unionpayPolicy,
    shopForm,
    merchantPreview,
  },
  created() {
    // if (this.$route.query.id) {
    //   this.dialogType = "edit";
    //   this.merchantId = this.$route.query.id;
    //   this.getFirstStep(this.merchantId);
    // } else {
    //   this.dialogType = "add";
    // }
  },
  watch: {
    "$route.query.a": {
      handler(val) {
        if (val) {
          const param = JSON.parse(this.$crypto.decrypt(val));
          this.dialogType = "add";
          const { merchantType } = param;
          this.merchantType = merchantType;
        }
      },
      immediate: true,
    },
    "$route.query.e": {
      handler(val) {
        if (val) {
          const param = JSON.parse(this.$crypto.decrypt(val));
          const { id } = param;
          this.dialogType = "edit";
          this.merchantId = id;
          this.getFirstStep(this.merchantId);
        }
      },
      immediate: true,
    },
    active(step) {
      if (step == 4) {
        this.dialogSubmitBtnText = "提 交";
      } else {
        this.dialogSubmitBtnText = "下一页";
      }
    }
  },
  methods: {
    /**
     * @func 暂存
     * @desc 普通商户五合一表单 暂时存储当前页面内容 不通过任何表单校验，直接提交表单内容至接口
     * */
    handlePageStorage() {
      const _this = this;
      switch (_this.active) {
        case 0:
          break;
        case 1:
          [
            _this.cardForm.openAccProvinceCode,
            _this.cardForm.openAccCityCode,
            _this.cardForm.openAccAreaCode,
          ] = _this.cardForm.accProvinceCityArea;
          _this.cardForm.id = _this.merchantId;
          _this.getStepSto(_this.cardForm);
          break;
        case 2:
          _this.rateForm.id = _this.merchantId;
          _this.rateForm.vo = _this.$refs["toRateForm"].data;
          addThirdStep(_this.rateForm).then((res) => {
              if (res.data.code == "200") {
                this.$message({
                  type: "success",
                  message: "信息暂存成功!",
                });
                this.$router.$avueRouter.closeTag();
                this.$router.back();
              } else {
                this.$message({
                  type: "error",
                  message: res.data.message,
                });
              }
            },
            (error) => {
              window.console.log(error);
            }
          );
          break;
        case 3:
          _this.shopForm.id = _this.merchantId;
          addFourthStep(_this.shopForm).then((res) => {
              if (res.data.code == "200") {
                this.$message({
                  type: "success",
                  message: "信息暂存成功!",
                });
                this.$router.$avueRouter.closeTag();
                this.$router.back();
              } else {
                this.$message({
                  type: "error",
                  message: res.data.message,
                });
              }
            },
            (error) => {
              window.console.log(error);
            }
          );
          break;
        case 4:
          break;
      }
    },
    /**
     * @func 上一页
     * @desc 普通商户五合一表单 上一页
     * */
    handlePageUp() {
      if (this.active-- <= 0) this.active = 0;
      // if (this.active == 0) {
      //   getFirstStepVO(id).then((res) => {
      //     this.basicForm.id = res.data.data;
      //   });
      // } else if (this.active == 1) {
      // } else if (this.active == 2) {
      //   getBankDetail(this.oem.tenantId, this.oem.userNo, this.oem.userType).then(res => {
      //     this.cardForm = res.data.data;
      //   });
      // }
    },
    /**
     * @func 下一页
     * @desc 普通商户五合一表单 下一页
     * */
    handlePageDown() {
      const _this = this;
      if (_this.active == 0) {
        _this.basicSubmit(_this.dialogType, 'page');
      } else if (_this.active == 1) {
        _this.cardSubmit(_this.dialogType, 'page');
      } else if (_this.active == 2) {
        _this.rateSubmit(_this.dialogType, 'page');
      } else if (_this.active == 3) {
        _this.shopSubmit(_this.dialogType, 'page');
      } else if (_this.active == 4) {
        _this.pullFlowSubmit();
      }
    },
    /**
		 * @func 跳一跳
		 * @desc 普通商户多合一表单 步骤跳转
		 * */
		jumpStepSkip(index) {
			const _this = this;
			if (_this.active < index) {
				switch (_this.active) {
					case 0:
						_this.basicSubmit(_this.dialogType, index);
						break;
					case 1:
						_this.cardSubmit(_this.dialogType, index);
						break;
					case 2:
						_this.rateSubmit(_this.dialogType, index);
						break;
					case 3:
						_this.shopSubmit(_this.dialogType, index);
						break;
          case 4:
            _this.pullFlowSubmit();
						break;
				}
			} else {
				_this.active = index;
			}
		},
    // /**
    //  * @func 五合一新增
    //  * @desc 点击新增初始化显示基本信息
    //  * */
    // handleSave() {
    //   this.active = 0;
    //   this.dialogVisible = true;
    //   this.dialogType = "add";
    //   this.dialogText = "新增";
    //   this.dialogSubmitBtnText = "下一页";
    //   // this.$refs.toBasicForm.$refs['mcc'].panel.store.nodes = null;
    // },
    // /**
    //  * @func 五合一编辑
    //  * @desc 点击编辑回显基本信息
    //  * @param {Object} row - 商户信息
    //  * */
    // handleEdit(row) {
    //   this.active = 0;
    //   this.dialogVisible = true;
    //   this.dialogType = "edit";
    //   this.dialogText = "编辑";
    //   this.dialogSubmitBtnText = "下一页";
    //   this.merchantId = row.id;
    //   if (row.id) {
    //     this.getFirstStep(this.merchantId);
    //   }
    // },
    /**
     * @func 越级跳回调详情
     * @desc 越级跳回调详情
     * @param {Object} row - 数据
     * */
    getLeapfrog(active, index) {
      const _this = this;
      if (index > (active + 1)) {
        switch (index) {
					case 0:
						_this.getFirstStep(_this.merchantId);
						break;
					case 1:
						_this.getSecondStep(_this.merchantId);
						break;
					case 2:
						_this.getThirdStep(_this.merchantId);
						break;
					case 3:
						_this.getFourthStep(_this.merchantId);
						break;
          case 4:
            _this.getFifthStep(_this.merchantId);
						break;
				}
        this.active = index;
      } else {
        this.active = index;
      }
    },
    /**
     * @func 暂存回调
     * @desc 暂存回调
     * @param {Object} row - 数据
     * */
    getStepSto(row) {
      saveStepSto(row).then(
        () => {
          this.$message({
            type: "success",
            message: "信息暂存成功!",
          });
          this.$router.$avueRouter.closeTag();
          this.$router.back();
        },
        (error) => {
          window.console.log(error);
        }
      );
    },
    /**
     * @func 第一步详情
     * @desc 第一步详情
     * @param {Number} id - key值
     * */
    getFirstStep(id) {
      getFirstStepVO(id).then((res) => {
        this.basicForm = res.data.data;
        this.merUserType = this.basicForm.userType;
        this.$refs["toBasicForm"].isShow = false;
        
        let skipStep = this.basicForm.incomingStep;
        if (this.basicForm.incomingStep == 2) {
          this.active = skipStep - 1;
          this.getSecondStep(this.merchantId);
        } else if (this.basicForm.incomingStep == 3) {
          this.active = skipStep - 1;
          this.getSecondStep(this.merchantId);
          // this.getThirdStep(this.merchantId);
          this.$refs["toRateForm"].getRateInfo();
        } else if (this.basicForm.incomingStep == 4) {
          this.active = skipStep - 1;
          this.getSecondStep(this.merchantId);
          // this.getThirdStep(this.merchantId);
          this.$refs["toRateForm"].getRateInfo();
          this.getFourthStep(this.merchantId);
        }
        // this.$refs.toBasicForm.mccProps.userType = this.basicForm.userType;
        if (JSON.stringify(res.data.data) !== "{}") {
          this.$nextTick(() => {
            this.$refs["toBasicForm"].isShow = true;
            this.basicForm.legalProvinceCityArea = [
              this.basicForm.legalProvince,
              this.basicForm.legalCity,
              this.basicForm.legalArea,
            ];
            this.basicForm.mercProvinceCityArea = [
              this.basicForm.mercProvCode,
              this.basicForm.mercCityCode,
              this.basicForm.mercAreaCode,
            ];
            this.$refs["toBasicForm"].roaLicenseFileList.push({
              url: res.data.data.licensePicUrl || "",
            });
            this.$refs["toBasicForm"].handHeldList.push({
              url: res.data.data.handHeldUrl || "",
            });
            this.$refs["toBasicForm"].cardFrontList.push({
              url: res.data.data.cardFrontUrl || "",
            });
            this.$refs["toBasicForm"].cardReverseList.push({
              url: res.data.data.cardReverseUrl || "",
            });
            this.$refs["toBasicForm"].authLetterPicList.push({
              url: res.data.data.authLetterPicUrl || "",
            });
            this.$refs["toBasicForm"].contactFrontPicList.push({
              url: res.data.data.contactFrontPicUrl || "",
            });
            this.$refs["toBasicForm"].contactReversePicList.push({
              url: res.data.data.contactReversePicUrl || "",
            });
          });
        }
      });
    },
    /**
     * @func 第二步详情
     * @desc 第二步详情
     * @param {Number} id - key值
     * */
    getSecondStep(id) {
      getSecondStepVO(id).then((res) => {
        let data = res.data.data;
        this.$refs["toCardForm"].isShow = false;
        if (JSON.stringify(data) !== "{}") {
          // 过滤结算类型列表是否有表单内结算类型的值 没有则赋为空
          if (
            this.$refs["toCardForm"].balanceAccTypeOptions.filter(
              (obj) => obj.dictKey == data.balanceAccType
            ).length == 0
          ) {
            data.balanceAccType = "";
          }
          // 图片回显
          this.$refs["toCardForm"].accountFrontList.length == 0 &&
          data.accountFrontUrl
            ? this.$refs["toCardForm"].accountFrontList.push({
                url: data.accountFrontUrl || "",
              })
            : this.$refs["toCardForm"].accountFrontList;
          this.$refs["toCardForm"].accountReverseList.length == 0 &&
          data.accountReverseUrl
            ? this.$refs["toCardForm"].accountReverseList.push({
                url: data.accountReverseUrl || "",
              })
            : this.$refs["toCardForm"].accountReverseList;
          this.$refs["toCardForm"].holdBillingAttorneyList.length == 0 &&
          data.billingAttorneyUrl
            ? this.$refs["toCardForm"].holdBillingAttorneyList.push({
                url: data.billingAttorneyUrl || "",
              })
            : this.$refs["toCardForm"].holdBillingAttorneyList;
          this.$refs["toCardForm"].openPermitList.length == 0 &&
          data.openPermitUrl
            ? this.$refs["toCardForm"].openPermitList.push({
                url: data.openPermitUrl || "",
              })
            : this.$refs["toCardForm"].openPermitList;
          this.$refs["toCardForm"].ecardFrontList.length == 0 &&
          data.ecardFrontUrl
            ? this.$refs["toCardForm"].ecardFrontList.push({
                url: data.ecardFrontUrl || "",
              })
            : this.$refs["toCardForm"].ecardFrontList;
          this.$refs["toCardForm"].ecardReverseList.length == 0 &&
          data.ecardReverseUrl
            ? this.$refs["toCardForm"].ecardReverseList.push({
                url: data.ecardReverseUrl || "",
              })
            : this.$refs["toCardForm"].ecardReverseList;

          this.$nextTick(() => {
            this.cardForm = data;
            this.$refs["toCardForm"].isShow = true;
            this.cardForm.accProvinceCityArea = [
              this.cardForm.openAccProvinceCode,
              this.cardForm.openAccCityCode,
              this.cardForm.openAccAreaCode,
            ];
          });
          if (data.openAccCityCode && data.bankType) {
            getSecondBankList(
              "-1",
              "-1",
              data.openAccCityCode,
              data.bankType
            ).then((res) => {
              this.$refs["toCardForm"].accBankNameOptions = res.data.data.records;
            });
          }
        }
      });
    },
    /**
     * @func 第三步详情
     * @desc 第三步详情
     * @param {Number} id - key值
     * */
    getThirdStep(id) {
      getThirdStepVO({id}).then((res) => {
        // this.rateForm = res.data.data;
        let data = res.data.data.vo;
        this.rateForm.id = this.merchantId;
        this.rateForm.vo = this.$refs["toRateForm"].data;

        for (let i = 0; i < data.length; i++) {
          for (let r = 0; r < this.rateForm.vo.length; r++) {
            if (data[i].aisleCode == this.rateForm.vo[r].aisleCode) {
              this.rateForm.vo[r].codeAdvanceRate = data[i].codeAdvanceRate;
              this.rateForm.vo[r].codeScanningRate = data[i].codeScanningRate;
            }
          }
        }
      });
    },
    /**
     * @func 第四步详情
     * @desc 第四步详情
     * @param {Number} id - key值
     * */
    getFourthStep(id) {
      getFourthStepVO(id).then((res) => {
        this.shopForm = res.data.data;
        if (JSON.stringify(res.data.data) !== "{}") {
          this.$refs["toShopForm"].merchantDoorList.length == 0 && res.data.data.merchantDoorUrl ? this.$refs["toShopForm"].merchantDoorList.push({ url: res.data.data.merchantDoorUrl }): this.$refs["toShopForm"].merchantDoorList;
          this.$refs["toShopForm"].interiorList.length == 0 && res.data.data.interiorUrl ? this.$refs["toShopForm"].interiorList.push({ url: res.data.data.interiorUrl }) : this.$refs["toShopForm"].interiorList;
          this.$refs["toShopForm"].checkoutCounterList.length == 0 && res.data.data.checkoutCounterUrl ? this.$refs["toShopForm"].checkoutCounterList.push({ url: res.data.data.checkoutCounterUrl }) : this.$refs["toShopForm"].checkoutCounterList;

          this.$refs["toShopForm"].acquiringList.length == 0 && res.data.data.acquiringUrl ? this.$refs["toShopForm"].acquiringList.push({ url: res.data.data.acquiringUrl }): this.$refs["toShopForm"].acquiringList;
          this.$refs["toShopForm"].stampList.length == 0 && res.data.data.stampUrl ? this.$refs["toShopForm"].stampList.push({ url: res.data.data.stampUrl }): this.$refs["toShopForm"].stampList;
          this.$refs["toShopForm"].businessList.length == 0 && res.data.data.businessUrl ? this.$refs["toShopForm"].businessList.push({ url: res.data.data.businessUrl }): this.$refs["toShopForm"].businessList;
          this.$refs["toShopForm"].leaseholdFirstList.length == 0 && res.data.data.leaseholdFirstUrl ? this.$refs["toShopForm"].leaseholdFirstList.push({ url: res.data.data.leaseholdFirstUrl }): this.$refs["toShopForm"].leaseholdFirstList;
          this.$refs["toShopForm"].leaseholdSecondList.length == 0 && res.data.data.leaseholdSecondUrl ? this.$refs["toShopForm"].leaseholdSecondList.push({ url: res.data.data.leaseholdSecondUrl }): this.$refs["toShopForm"].leaseholdSecondList;
          this.$refs["toShopForm"].leaseholdThirdList.length == 0 && res.data.data.leaseholdThirdUrl ? this.$refs["toShopForm"].leaseholdThirdList.push({ url: res.data.data.leaseholdThirdUrl }): this.$refs["toShopForm"].leaseholdThirdList;
          this.$refs["toShopForm"].areaPicList.length == 0 && res.data.data.areaPicUrl ? this.$refs["toShopForm"].areaPicList.push({ url: res.data.data.areaPicUrl }): this.$refs["toShopForm"].areaPicList;
        }
      });
    },
    /**
     * @func 第五步详情
     * @desc 第五步详情
     * @param {Number} id - key值
     * */
    getFifthStep(id) {
      getFifthStepVO(id).then((res) => {
        this.merchantForm = res.data.data;
        // if (JSON.stringify(res.data.data) !== "{}") {}
      });
    },
    /**
     * @func 获取费率组件传值
     * @desc 获取费率组件传值
     * @param {String} val - key值
     * */
    getCodeAdvanceRate(val) {
      this.codeAdvanceRate = val;
    },
    /**
     * @func 商户基本信息提交
     * @desc 提交基本信息
     * @param {String} type - 类型(新增/修改)
     * @param {String} index - 索引状态(page/index)
     * */
    basicSubmit(type, index) {
      if (type === "add") {
        this.$refs["toBasicForm"].$refs["basicForm"].validate((valid) => {
          if (valid) {
            [
              this.basicForm.mercProvCode,
              this.basicForm.mercCityCode,
              this.basicForm.mercAreaCode,
            ] = this.basicForm.mercProvinceCityArea;
            [
              this.basicForm.legalProvince,
              this.basicForm.legalCity,
              this.basicForm.legalArea,
            ] = this.basicForm.legalProvinceCityArea;
            this.basicForm.id = this.merchantId;
            const merchantType = this.merchantType;
            addFirstStep({merchantType, ...this.basicForm}).then(
              (res) => {
                this.merchantId = res.data.data;
                // this.getPolicy();
                // this.$refs["toRateForm"].getRateInfo();
                if (res.data.code == "200") {
                  this.$message({
                    type: "success",
                    message: "操作成功!",
                  });
                  if (index == 'page') {
                    if (this.active++ > 4) this.active = 0;
                  } else {
                    this.getLeapfrog(this.active, index);
                  }
                  if (this.basicForm.userType == "3") {
                    this.$refs["toCardForm"].cardForm.balanceAccName = this.basicForm.legalPersonName;
                  } else {
                    this.$refs["toCardForm"].cardForm.balanceAccName = this.basicForm.businessLicence;
                  }
                } else {
                  this.$message({
                    type: "error",
                    message: res.data.message,
                  });
                }
              },
              (error) => {
                window.console.log(error);
              }
            );
          } else {
            return false;
          }
        });
      } else if (type === "edit") {
        this.$refs["toBasicForm"].$refs["basicForm"].validate((valid) => {
          if (valid) {
            [
              this.basicForm.mercProvCode,
              this.basicForm.mercCityCode,
              this.basicForm.mercAreaCode,
            ] = this.basicForm.mercProvinceCityArea;
            [
              this.basicForm.legalProvince,
              this.basicForm.legalCity,
              this.basicForm.legalArea,
            ] = this.basicForm.legalProvinceCityArea;
            updateFirstStep(this.basicForm).then(
              (res) => {
                this.merchantId = res.data.data;
                // this.getPolicy();
                if (res.data.code == "200") {
                  if (this.merchantId) {
                    this.getSecondStep(this.merchantId);
                  }
                  this.$message({
                    type: "success",
                    message: "操作成功!",
                  });
                  if (index == 'page') {
                    if (this.active++ > 4) this.active = 0;
                  } else {
                    this.getLeapfrog(this.active, index);
                  }
                } else {
                  this.$message({
                    type: "error",
                    message: res.data.message,
                  });
                }
              },
              (error) => {
                window.console.log(error);
              }
            );
          } else {
            return false;
          }
        });
      }
    },
    /**
     * @func 商户结算卡信息提交
     * @desc 提交结算卡信息
     * @param {String} type - 类型(新增/修改)
     * @param {String} index - 索引状态(page/index)
     * */
    cardSubmit(type, index) {
      if (type === "add") {
        this.$refs["toCardForm"].$refs["cardForm"].validate((valid) => {
          if (valid) {
            [
              this.cardForm.openAccProvinceCode,
              this.cardForm.openAccCityCode,
              this.cardForm.openAccAreaCode,
            ] = this.cardForm.accProvinceCityArea;
            this.cardForm.id = this.merchantId;
            addSecondStep(this.cardForm).then(
              (res) => {
                if (res.data.code == "200") {
                  this.$refs["toRateForm"].getRateInfo();
                  this.$message({
                    type: "success",
                    message: "操作成功!",
                  });
                  if (index == 'page') {
                    if (this.active++ > 4) this.active = 0;
                  } else {
                    this.getLeapfrog(this.active, index);
                  }
                } else {
                  this.$message({
                    type: "error",
                    message: res.data.message,
                  });
                }
              },
              (error) => {
                window.console.log(error);
              }
            );
          } else {
            return false;
          }
        });
      } else if (type === "edit") {
        this.$refs["toCardForm"].$refs["cardForm"].validate((valid) => {
          if (valid) {
            [
              this.cardForm.openAccProvinceCode,
              this.cardForm.openAccCityCode,
              this.cardForm.openAccAreaCode,
            ] = this.cardForm.accProvinceCityArea;
            this.cardForm.id = this.merchantId;
            updateSecondStep(this.cardForm).then(
              (res) => {
                if (res.data.code == "200") {
                  if (this.merchantId) {
                    // this.getThirdStep(this.merchantId);
                    this.$refs["toRateForm"].getRateInfo();
                  }
                  this.$message({
                    type: "success",
                    message: "操作成功!",
                  });
                  if (index == 'page') {
                    if (this.active++ > 4) this.active = 0;
                  } else {
                    // this.active = index;
                    this.getLeapfrog(this.active, index);
                  }
                } else {
                  this.$message({
                    type: "error",
                    message: res.data.message,
                  });
                }
              },
              (error) => {
                window.console.log(error);
              }
            );
          } else {
            return false;
          }
        });
      }
    },
    /**
     * @func 商户费率信息提交
     * @desc 提交费率信息
     * @param {String} type - 类型(新增/修改)
     * @param {String} index - 索引状态(page/index)
     * */
    rateSubmit(type, index) {
      if (type === "add") {
        this.rateForm.id = this.merchantId;
        this.rateForm.vo = this.$refs["toRateForm"].data;
        // if (!this.rateForm.codeAdvanceOpen) {
        //   this.rateForm.codeAdvanceOpen = "0";
        // }
        addThirdStep(this.rateForm).then(
          (res) => {
            if (res.data.code == "200") {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              if (index == 'page') {
                if (this.active++ > 4) this.active = 0;
              } else {
                this.getLeapfrog(this.active, index);
              }
            } else {
              this.$message({
                type: "error",
                message: res.data.message,
              });
            }
          },
          (error) => {
            window.console.log(error);
          }
        );
      } else if (type === "edit") {
        this.rateForm.id = this.merchantId;
        this.rateForm.vo = this.$refs["toRateForm"].data;
        // if (!this.rateForm.codeAdvanceOpen) {
        //   this.rateForm.codeAdvanceOpen = "0";
        // }
        updateThirdStep(this.rateForm).then(
          (res) => {
            if (res.data.code == "200") {
              if (this.merchantId) {
                this.getFourthStep(this.merchantId);
              }
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              if (index == 'page') {
                if (this.active++ > 4) this.active = 0;
              } else {
                this.getLeapfrog(this.active, index);
              }
            } else {
              this.$message({
                type: "error",
                message: res.data.message,
              });
            }
          },
          (error) => {
            window.console.log(error);
          }
        );
      }
    },
    /**
     * @func 商户门店信息提交
     * @desc 提交门店信息
     * @param {String} type - 类型(新增/修改)
     * @param {String} index - 索引状态(page/index)
     * */
    shopSubmit(type, index) {
      if (type === "add") {
        this.$refs["toShopForm"].$refs["shopForm"].validate((valid) => {
          if (valid) {
            this.shopForm.id = this.merchantId;
            addFourthStep(this.shopForm).then(
              (res) => {
                if (res.data.code == "200") {
                  if (this.merchantId) {
                    this.getFifthStep(this.merchantId);
                  }
                  this.$message({
                    type: "success",
                    message: "操作成功!",
                  });
                  if (index == 'page') {
                    if (this.active++ > 4) this.active = 0;
                  } else {
                    this.getLeapfrog(this.active, index);
                  }
                } else {
                  this.$message({
                    type: "error",
                    message: res.data.message,
                  });
                }
              },
              (error) => {
                window.console.log(error);
              }
            );
          } else {
            return false;
          }
        });
      } else if (type === "edit") {
        this.$refs["toShopForm"].$refs["shopForm"].validate((valid) => {
          if (valid) {
            this.shopForm.id = this.merchantId;
            updateFourthStep(this.shopForm).then(
              (res) => {
                if (res.data.code == "200") {
                  if (this.merchantId) {
                    this.getFifthStep(this.merchantId);
                  }
                  this.$message({
                    type: "success",
                    message: "操作成功!",
                  });
                  if (index == 'page') {
                    if (this.active++ > 4) this.active = 0;
                  } else {
                    this.getLeapfrog(this.active, index);
                  }
                } else {
                  this.$message({
                    type: "error",
                    message: res.data.message,
                  });
                }
              },
              (error) => {
                window.console.log(error);
              }
            );
          } else {
            return false;
          }
        });
      }
    },
    /* 政策详情 */
    getPolicy() {
      getThirdStepAttachPlus(this.merchantId).then((res) => {
        if (res.data.code == "200") {
          let datas = res.data.data;
          for (let i = 0; i < datas.length; i++) {
            this.$set(datas[i], "isShow", true);
            this.$set(datas[i], "templatePolNo", ""); //接口传值模板编号
            this.$set(datas[i], "templateData", "");  //已选模板数据
            // this.$set(datas[i], "codeAdvanceRateMin", Number(resHig.data.data.codeAdvanceRate));  //配置最低可设置垫资费率
            // this.$set(datas[i], "aptRateMin", Number(resHig.data.data.aptRate));  //配置最低可设置成本费率
          }
          this.$nextTick(()=>{
            this.unionpayData = datas;
          })
          // this.rateForm.codeAdvanceRate = res.data.data.codeAdvanceRate;
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
      });
    },
    /**
     * @func 商户预览信息提交-拉取工作流
     * @desc 预览信息提交拉取工作流
     * */
    pullFlowSubmit() {
      getFifthFlow(this.merchantId).then(
        (res) => {
          if (res.data.code == "200") {
            this.$message({
              type: "success",
              message: "商户进件信息提交成功!",
            });
            this.$router.$avueRouter.closeTag();
            this.$router.back();
            // this.emptyForm();
            // this.dialogVisible = false;
            // this.onLoad(this.page);
          } else {
            this.$message({
              type: "error",
              message: res.data.message,
            });
          }
        },
        (error) => {
          window.console.log(error);
        }
      );
    },
    getUserType(val) {
      this.merUserType = val;
    },
    /* 返回页面 */
    goBackPage() {
      if (this.active != 0 && this.active != 4) {
        this.$confirm('您还未填写完商户信息，确定暂存后返回吗?', '瑞生意提示', {
          confirmButtonText: '暂存并返回',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //暂存方法
          this.handlePageStorage();
        }).catch(() => {
                 
        });
      } else {
        this.$router.$avueRouter.closeTag();
        this.$router.back();
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.merchant-net-steps {
  background-color: #F8F9FD;
  border-radius: 20px;
  padding: 50px 200px;
  margin-bottom: 50px;
  user-select: none;
}

.merchant-net-pointer {
  cursor: pointer;
}
</style>